package com.fortune.controller;


import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.constants.enums.Shelf;
import com.fortune.dto.excution.ProductCategoryExcution;
import com.fortune.dto.excution.ProductExcution;
import com.fortune.dto.in.product.ProductAddDto;
import com.fortune.dto.in.product.ProductEditDto;
import com.fortune.dto.in.product.ProductListDto;
import com.fortune.dto.in.product.ProductUpAndDown;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.ProductCategoryEnum;
import com.fortune.enums.ProductEnum;
import com.fortune.service.MProductInfoService;
import com.fortune.service.MSpecificationService;
import com.fortune.service.MUnitService;
import com.fortune.service.ProductCategoryService;
import com.fortune.vo.mSpecification.MSpecification;
import com.fortune.vo.mUnit.MUnit;
import com.fortune.vo.productCategory.ProductCategory;
import com.fortune.vo.productCategoryMgt.ProductCategoryVo;
import com.fortune.vo.productMgt.Product;
import com.fortune.vo.productStockDetailInfo.ProductStockDetailInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(value = "product", description = "商品管理", tags = {"商品管理相关"})
@RestController
@RequestMapping("product")
public class ProductController {
	@Autowired
	private MProductInfoService mProductInfoService;
	@Autowired
	private MSpecificationService mSpecificationService;
	@Autowired
	private MUnitService mUnitService;
	@Autowired
	private ProductCategoryService productCategoryService;

//	@ApiOperation(value = "商品列表", nickname = "productList", notes = "" ,tags = {"@石匡"})
//	@ApiImplicitParams({
//			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
//			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
//			@ApiImplicitParam(name="ppcid", value="商品分类ID",paramType="query",dataType="string")
//	})
//	@GetMapping(value = "list",
//        produces = { MediaType.APPLICATION_JSON_VALUE })
//	public BaseAPIExcution<PagingData<Product>> list(HttpServletRequest request,@ApiIgnore @Validated ProductListDto ProductListDto) {
//		HttpSession session = request.getSession();
//		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
//		String companyId = mUserInfo.getmId();
//		ProductListDto.setPage((ProductListDto.getPage()-1)*10);
//		List<Product> productList = mProductInfoService.queryProductInfoList(companyId,ProductListDto);
//		int count = mProductInfoService.queryProductInfoListCount(companyId,ProductListDto);
//		PagingData<Product> pagingData = new PagingData<>();
//		pagingData.setData(productList);
//		pagingData.setRecordsTotal(count);
//		return new ProductExcution<PagingData<Product>>(pagingData, ProductEnum.SUCCESS);
//	}

	@ApiOperation(value = "添加商品", nickname = "mProductAdd", notes = "" ,tags = {"@石匡"})
	@PostMapping(value = "add",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody ProductAddDto addDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = mProductInfoService.queryProductByName(companyId,addDto.getName());
		if (num > 0){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_EXIST);
		}
		num = mProductInfoService.queryProductByNumber(companyId,addDto.getNumber());
		if (num > 0){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_EXIST);
		}
		num = mProductInfoService.addProduce(companyId,addDto);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.FAILURE);
		}
		return new ProductExcution<Void>(null, ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "编辑商品", nickname = "mProductEdit", notes = "" ,tags = {"@石匡"})
	@PutMapping(value = "edit",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody ProductEditDto editDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = queryProductByProduceId(companyId,editDto.getId());
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_NO_EXIST);
		}
		num = mProductInfoService.queryEditProductByNameOrNumber(companyId,editDto);
		if (num > 0){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_EXIST);
		}
		num = mProductInfoService.editProductByProduceIdAndProduceId(companyId,editDto);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.FAILURE);
		}
		return new ProductExcution<Void>(null,ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "删除商品", nickname = "mProductDelete", notes = "" ,tags = {"@石匡"})
	@ApiImplicitParam(name="produceId", value="商品id",paramType="query",dataType="string")
	@DeleteMapping(value = "delete",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotEmpty(message="商品id不能为空") String produceId) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = queryProductByProduceId(companyId,produceId);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_NO_EXIST);
		}
		ProductStockDetailInfo detailInfo = mProductInfoService.queryIsStockExist(companyId,produceId);
		if (detailInfo != null){
			int remainNum = detailInfo.getRemainNum();
			if (remainNum > 0){
				return new ProductExcution<Void>(null,ProductEnum.PRODUCE_STOCK_EXIST);
			}
		}
		num = mProductInfoService.deleteProduce(companyId,produceId);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.FAILURE);
		}
		return new ProductExcution<Void>(null,ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "上下架商品", nickname = "mProductUpAndDown", notes = "" ,tags = {"@石匡"})
	@PutMapping(value = "mProductUpAndDown",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> upAndDown(HttpServletRequest request,@Validated @RequestBody ProductUpAndDown upAndDown) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
//		int num = queryProductByProduceId(companyId,upAndDown.getId());
//		if (num < 1){
//			return new ProductExcution(null,ProductEnum.PRODUCE_NO_EXIST);
//		}
		Product product = mProductInfoService.queryProduceInfo(companyId,upAndDown.getId());
		if (product==null){
			return new ProductExcution(null,ProductEnum.PRODUCE_NO_EXIST);
		}
		Shelf shelf = product.getShelf();
		if (shelf.toString().equals(upAndDown.getType().toString()) && upAndDown.getType().toString().equals("YES")){
			return new ProductExcution(null,ProductEnum.UP);
		}
		if (shelf.toString().equals(upAndDown.getType().toString()) && upAndDown.getType().toString().equals("NO")){
			return new ProductExcution(null,ProductEnum.DOWN);
		}
		int num = mProductInfoService.upAndDownProduce(companyId,upAndDown);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.FAILURE);
		}
		return new ProductExcution<Void>(null,ProductEnum.SUCCESS);
	}

	private int queryProductByProduceId(String companyId,String produceId){
		return mProductInfoService.queryProductByProduceId(companyId,produceId);
	}

	@ApiOperation(value = "规格型号", nickname = "mSpecificationList", notes = "" ,tags = {"@石匡"})
	@GetMapping(value = "mSpecificationList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<MSpecification>> mSpecificationList(HttpServletRequest request) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		List<MSpecification> list = mSpecificationService.queryProduceMSpecificationList(companyId);
		return new ProductExcution<List<MSpecification>>(list, ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "单位", nickname = "mUnitList", notes = "" ,tags = {"@石匡"})
	@GetMapping(value = "mUnitList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<MUnit>> mUnitList(HttpServletRequest request) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		List<MUnit> list = mUnitService.queryProduceMunit(companyId);
		return new ProductExcution<List<MUnit>>(list, ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "商品分类（平台统一分类）", nickname = "productCategoryList", notes = "" ,tags = {"@石匡"})
	@GetMapping(value = "productCategoryList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<ProductCategory>> productCategory() {
		List<ProductCategory> list = productCategoryService.queryProductCategory();
		return new ProductExcution<List<ProductCategory>>(list, ProductEnum.SUCCESS);
	}



	@ApiOperation(value = "获取所有分类（树形结构）", nickname = "productCategoryMgtList", notes = "", tags={ "@石匡" })
	@GetMapping(value = "cateList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<ProductCategoryVo>> list() {
		List<ProductCategoryVo> categories = productCategoryService.getTreeCategories();
		return new ProductCategoryExcution<List<ProductCategoryVo>>(categories, ProductCategoryEnum.SUCCESS);
	}


	@ApiOperation(value = "商品列表", nickname = "productList", notes = "" ,tags = {"@石匡"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
			@ApiImplicitParam(name="ppcid", value="商品分类ID",paramType="query",dataType="string"),
			@ApiImplicitParam(name="search", value="搜索",paramType="query",dataType="string")
	})
	@GetMapping(value = "list",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<Product>> list(HttpServletRequest request,@ApiIgnore @Validated ProductListDto productListDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		productListDto.setPage((productListDto.getPage()-1)*10);

		int num = mProductInfoService.queryProductCategory(productListDto.getPpcid());
		List<Product> productList = new ArrayList<>();
		int count = 0;
		if (num > 0){
			productList = mProductInfoService.queryProductInfoFuList(companyId,productListDto);
			count = mProductInfoService.queryProductInfoListFuCount(companyId,productListDto);
		}else {
			productList = mProductInfoService.queryProductInfoList(companyId,productListDto);
			count = mProductInfoService.queryProductInfoListCount(companyId,productListDto);
		}

		PagingData<Product> pagingData = new PagingData<>();
		pagingData.setData(productList);
		pagingData.setRecordsTotal(count);
		return new ProductExcution<PagingData<Product>>(pagingData, ProductEnum.SUCCESS);
	}
}
